<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/21
 * Time: 19:01
 */

namespace App\Game\Traits;

use Crypt_RSA;

trait EbetTrait
{
    public function getSign($plaintext)
    {
        $rsa = new Crypt_RSA();
        $rsa->loadKey($this->privateKey);//xxx 私钥匙
        $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
        $rsa->setHash("md5");
        $signature = $rsa->sign($plaintext);
        return base64_encode($signature);
    }

    protected function getToken($params)
    {
        $rsa = new Crypt_RSA();
        $rsa->loadKey($this->privateKey);
        return $rsa->encrypt(json_encode($params));
    }

    protected function decryptToken($token)
    {
        $rsa = new Crypt_RSA();
        $rsa->loadKey($this->publicKey);
        return json_decode($rsa->decrypt($token), true);
    }

    public function apiDataParse(array $apiData, array $data)
    {
        $data = $this->gameCode($data);
        return $data;
    }

    private function gameCode(array $data)
    {
        if (array_key_exists('game_code', $data)) {
            $data['game_code'] = str_before($data['game_code'], '-');
        }
        return $data;
    }
}